package com.zhenyi.toupiao.interceptor;

import com.cjw.model.DBMap;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import com.utils.AuthUtils;

public class AdminInterceptor implements Interceptor {

	private void renderLogin(Controller controller) {
		DBMap map = new DBMap();
		map.put("status", 401);
		map.put("msg", "认证失败");
		controller.renderJson(map);
	}

	@Override
	public void intercept(Invocation inv) {
		Controller controller = inv.getController();
		try {
			String uid = AuthUtils.getTokenUid(controller);
			if (StrKit.notBlank(uid)) {
				inv.invoke();
			} else {
				renderLogin(controller);
				return;
			}
		} catch (Exception e) {
			e.printStackTrace();
			DBMap map = new DBMap();
			map.put("status", 500);
			map.put("msg", "出错");
			controller.renderJson(map);
		}
	}

}
